package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.analyst.spatialanalyst.GeneralizeAnalyst;
import com.supermap.analyst.spatialanalyst.ResampleMode;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class RasterResamplingDialog extends CJDialog {
    private Workspace workspace;
    private JComboBox<String> srcDatasourceCmb;
    private JComboBox<String> srcDatasetCmb;
    private JComboBox<String> retDatasourceCmb;
    private JTextField retDatasetTxt;
    private JTextField rowsTxt;
    private JTextField columnsTxt;
    private JComboBox<ResampleMode> resampleModeCmb;
    private JTextField equantNumberTxt;
    private JTextField retRowsTxt;
    private JTextField retColumnsTxt;

    private ProcessBarDialog processBarDialog;

    public RasterResamplingDialog(Frame owner, String title, boolean modal, Workspace workspace) {
        super(owner, title, modal, 600, 300, new Object[]{workspace});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        workspace = (Workspace) args[0];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        add(centerPanel, BorderLayout.CENTER);

        JPanel srcDataPanel = new JPanel();
        srcDataPanel.setBorder(BorderFactory.createTitledBorder("源数据"));
        srcDataPanel.setLayout(new GridBagLayout());
        centerPanel.add(srcDataPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel srcDatasourceLbl = new JLabel("数据源:");
        srcDataPanel.add(srcDatasourceLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        srcDatasourceCmb = new JComboBox<>();
        Datasources datasources = workspace.getDatasources();
        int datasourcesCount = datasources.getCount();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                srcDatasourceCmb.addItem(datasource.getAlias());
            }
            srcDatasourceCmb.setSelectedIndex(0);
        }
        srcDatasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
        });
        srcDataPanel.add(srcDatasourceCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel srcDatasetLbl = new JLabel("数据集:");
        srcDataPanel.add(srcDatasetLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        srcDatasetCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            String selectedDatasourceName = (String) srcDatasourceCmb.getSelectedItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
            srcDatasetCmb.setSelectedIndex(0);
        }
        srcDatasetCmb.addItemListener(e -> {
            String selectedDatasetName = (String) e.getItem();
            if (datasourcesCount > 0) {
                Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
                Datasets datasets = datasource.getDatasets();
                Dataset dataset = datasets.get(selectedDatasetName);
                if (dataset instanceof DatasetImage) {
                    DatasetImage datasetImage = (DatasetImage) dataset;
                    rowsTxt.setText(String.valueOf(datasetImage.getHeight()));
                    columnsTxt.setText(String.valueOf(datasetImage.getWidth()));
                }
            }
        });
        srcDatasetCmb.setSelectedIndex(0);
        srcDataPanel.add(srcDatasetCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JPanel retDataPanel = new JPanel();
        retDataPanel.setBorder(BorderFactory.createTitledBorder("结果数据"));
        retDataPanel.setLayout(new GridBagLayout());
        centerPanel.add(retDataPanel, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retDatasourceLbl = new JLabel("数据源:");
        retDataPanel.add(retDatasourceLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retDatasourceCmb = new JComboBox<>();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                retDatasourceCmb.addItem(datasource.getAlias());
            }
            retDatasourceCmb.setSelectedIndex(0);
        }
        retDatasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(srcDatasetCmb, datasource);
        });
        retDataPanel.add(retDatasourceCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retDatasetLbl = new JLabel("数据集:");
        retDataPanel.add(retDatasetLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retDatasetTxt = new JTextField();
        if (datasourcesCount > 0) {
            String selectedDatasourceName = (String) retDatasourceCmb.getSelectedItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            Datasets datasets = datasource.getDatasets();
            String availableDatasetName = datasets.getAvailableDatasetName("EquantResampleResult");
            retDatasetTxt.setText(availableDatasetName);
        }
        retDataPanel.add(retDatasetTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JPanel srcDataInfoPanel = new JPanel();
        srcDataInfoPanel.setBorder(BorderFactory.createTitledBorder("源数据信息"));
        srcDataInfoPanel.setLayout(new GridBagLayout());
        centerPanel.add(srcDataInfoPanel, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel rowsLbl = new JLabel("行数:");
        srcDataInfoPanel.add(rowsLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        rowsTxt = new JTextField();
        rowsTxt.setEnabled(false);
        srcDataInfoPanel.add(rowsTxt, new GridBagConstraints(1, 0, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel columnsLbl = new JLabel("行数:");
        srcDataInfoPanel.add(columnsLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        columnsTxt = new JTextField();
        columnsTxt.setEnabled(false);
        srcDataInfoPanel.add(columnsTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        if (datasourcesCount > 0) {
            Datasource datasource = datasources.get((String) srcDatasourceCmb.getSelectedItem());
            Datasets datasets = datasource.getDatasets();
            Dataset dataset = datasets.get((String) srcDatasetCmb.getSelectedItem());
            if (dataset instanceof DatasetImage) {
                DatasetImage datasetImage = (DatasetImage) dataset;
                rowsTxt.setText(String.valueOf(datasetImage.getHeight()));
                columnsTxt.setText(String.valueOf(datasetImage.getWidth()));
            }
        }

        JPanel resampleParametersPanel = new JPanel();
        resampleParametersPanel.setBorder(BorderFactory.createTitledBorder("参数设置"));
        resampleParametersPanel.setLayout(new GridBagLayout());
        centerPanel.add(resampleParametersPanel, new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel resampleModeLbl = new JLabel("采样模式:");
        resampleParametersPanel.add(resampleModeLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        resampleModeCmb = new JComboBox<>();
        Enum[] enums = ResampleMode.getEnums(ResampleMode.class);
        for (Enum anEnum : enums) {
            resampleModeCmb.addItem((ResampleMode) anEnum);
        }
        resampleModeCmb.setSelectedIndex(0);
        resampleParametersPanel.add(resampleModeCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel equantNumberLbl = new JLabel("等分级数:");
        resampleParametersPanel.add(equantNumberLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        equantNumberTxt = new JTextField();
        equantNumberTxt.setText("2");
        resampleParametersPanel.add(equantNumberTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retRowsLbl = new JLabel("行数:");
        resampleParametersPanel.add(retRowsLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retRowsTxt = new JTextField();
        retRowsTxt.setEnabled(false);
        retRowsTxt.setText(String.valueOf(Integer.parseInt(rowsTxt.getText()) / 2));
        resampleParametersPanel.add(retRowsTxt, new GridBagConstraints(1, 2, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JLabel retColumnsLbl = new JLabel("列数:");
        resampleParametersPanel.add(retColumnsLbl, new GridBagConstraints(0, 3, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        retColumnsTxt = new JTextField();
        retColumnsTxt.setEnabled(false);
        retColumnsTxt.setText(String.valueOf(Integer.parseInt(columnsTxt.getText()) / 2));
        resampleParametersPanel.add(retColumnsTxt, new GridBagConstraints(1, 3, 1, 1, 10, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
    }

    @Override
    protected void doConfirmCore() {
        if (processBarDialog == null) {
            processBarDialog = new ProcessBarDialog((Frame) getOwner(), "栅格等分重采样", true);
            processBarDialog.setValue(0);
        }

        Datasource srcDatasource = workspace.getDatasources().get((String) srcDatasourceCmb.getSelectedItem());
        Dataset srcDataset = srcDatasource.getDatasets().get((String) srcDatasetCmb.getSelectedItem());
        Datasource retDatasource = workspace.getDatasources().get((String) retDatasourceCmb.getSelectedItem());
        int equantNumber = Integer.parseInt(equantNumberTxt.getText());
        ResampleMode resampleMode = (ResampleMode) resampleModeCmb.getSelectedItem();

        new Thread(() -> GeneralizeAnalyst.equantResample(srcDataset, equantNumber, resampleMode, retDatasource, retDatasetTxt.getText(), steppedEvent -> {
            int percent = steppedEvent.getPercent();
            processBarDialog.setValue(percent);
            if (percent == 100) {
                processBarDialog.setVisible(false);
                processBarDialog.dispose();
                processBarDialog = null;
            }
        })).start();
        processBarDialog.setVisible(true);
        setVisible(false);
    }

    private void loadDatasets(JComboBox<String> srcDatasetCmb, Datasource datasource) {
        srcDatasetCmb.removeAll();
        Datasets datasets = datasource.getDatasets();
        int datasetCount = datasets.getCount();
        for (int i = 0; i < datasetCount; i++) {
            Dataset dataset = datasets.get(i);
            DatasetType datasetType = dataset.getType();
            if (datasetType == DatasetType.GRID || datasetType == DatasetType.GRIDCOLLECTION
                    || datasetType == DatasetType.IMAGE || datasetType == DatasetType.IMAGECOLLECTION) {
                srcDatasetCmb.addItem(dataset.getName());
            }
        }
    }
}
